#!/bin/sh

MODULE_ROOT="$1"
NGX_OBJS="$2"
NGINX="$NGX_OBJS/nginx"
PIDFILE="/tmp/nginx-js.pid"
TIME=$(date +%s)
PADDING="$MODULE_ROOT/padding.txt"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib/:/opt/lib/"

if [ "$WITH_MASTER" = "yes" ]; then
	NGINX_OPTS=""
else
	NGINX_OPTS="daemon off; master_process off;"
fi

mkdir -p $MODULE_ROOT/logs
cat $PADDING >> $MODULE_ROOT/logs/error.log

if [ -f $PIDFILE ]; then
	PID=$(cat $PIDFILE)
	kill -QUIT $PID
	sleep 1
	if [ -f $PIDFILE ]; then
		kill -KILL $PID
	fi
fi

# enable core dumps
ulimit -c unlimited

# gdb --args
$NGINX -p $MODULE_ROOT -c nginx.conf -g "$NGINX_OPTS" &
sleep 1

OPTS="-m 5 --connect-timeout 1" # --http1.0

curl $OPTS "http://127.0.0.1:19090/run/nginx-object?prefix=$MODULE_ROOT&pid=$(cat $PIDFILE)"
curl $OPTS "http://127.0.0.1:19090/run/request-object?nginx=yes&uri=/run/request-object&method=GET&internal=false"
curl $OPTS "http://127.0.0.1:19090/rewrite/run/request-object?nginx=yes&uri=/run/request-object&method=GET&internal=true"
curl $OPTS --data "@${MODULE_ROOT}request-body-file.txt" "http://127.0.0.1:19090/run/request-body-discard"
curl $OPTS --data "some text body, ля-ля-ля" "http://127.0.0.1:19090/run/request-body?body=some+text+body,+ля-ля-ля"
curl $OPTS --data "some text body, ля-ля-ля" "http://127.0.0.1:19090/run/request-body-plain?body=some+text+body,+ля-ля-ля"
curl $OPTS --http1.0 --data "@${MODULE_ROOT}request-body-file.txt" "http://127.0.0.1:19090/run/request-body-file?body=some+text+body,+ля-ля-ля"
curl $OPTS http://127.0.0.1:19090/run/file
mkdir -p   $MODULE_ROOT/nginx-dir-tests/a/b/c
echo 123 > $MODULE_ROOT/nginx-dir-tests/a/b/f123
echo 2   > $MODULE_ROOT/nginx-dir-tests/a/b/c/f2
mkfifo     $MODULE_ROOT/nginx-dir-tests/a/fifo1
mkfifo     $MODULE_ROOT/nginx-dir-tests/a/b/fifo2
curl $OPTS http://127.0.0.1:19090/run/dir
rm -rf     $MODULE_ROOT/nginx-dir-tests/
if [ "$WITH_MASTER"  = "yes" ]; then
curl $OPTS "http://127.0.0.1:19090/run/process-cycle?fname=process-cycle-worker-exited-$TIME.txt"
fi
curl $OPTS --cookie "foo1=bar1; foo2=bar2" "http://127.0.0.1:19090/run/request-cookies?count=2&name1=foo1&value1=bar1&name2=foo2&value2=bar2"
BIG_STRING="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # 100
curl $OPTS --cookie "foo1=$BIG_STRING" "http://127.0.0.1:19090/run/request-cookies?count=1&name1=foo1&value1=$BIG_STRING"
curl $OPTS -H "Foo1: bar1" -H "Foo2: bar2" 'http://127.0.0.1:19090/run/request-headers-in?\{"Foo1":"bar1","Foo2":"bar2"\}'
curl $OPTS -H "Cookie: foo1=bar1; foo2=bar2" 'http://127.0.0.1:19090/run/request-headers-in?\{"Cookie":"foo1=bar1;+foo2=bar2"\}'
curl $OPTS -H "Big: $BIG_STRING" "http://127.0.0.1:19090/run/request-headers-in?\{\"Big\":\"$BIG_STRING\"\}"
curl $OPTS -A "Tester/1.0" "http://127.0.0.1:19090/run/request-headers-in?\{\"Host\":\"127.0.0.1:19090\",\"User-Agent\":\"Tester/1.0\"\}"
curl $OPTS --data "12345" "http://127.0.0.1:19090/run/request-headers-in?\{\"Content-Length\":5,\"Content-Type\":\"application/x-www-form-urlencoded\"\}"
curl $OPTS -r 3-55 "http://127.0.0.1:19090/run/request-headers-in?\{\"Range\":\"bytes=3-55\"\}"
curl $OPTS "http://127.0.0.1:19090/run/request-headers-in-set-get"
curl $OPTS "http://127.0.0.1:19090/run/request-headers-out-set-get"
curl $OPTS "http://127.0.0.1:19090/variable-set" # will be rewritten to th /run/variable-set
curl $OPTS "http://127.0.0.1:19090/run/request-variables"
curl $OPTS "http://127.0.0.1:19090/run/standart-classes"
curl $OPTS "http://127.0.0.1:19090/run/subrequests"
curl $OPTS "http://127.0.0.1:19090/run/subrequest-headers-out"
curl $OPTS "http://127.0.0.1:19090/run/subrequest-headers-in"
curl $OPTS "http://127.0.0.1:19090/run/request-sendfile"
curl $OPTS "http://127.0.0.1:19090/run/request-redirect"
curl $OPTS "http://127.0.0.1:19090/run/request-send-string"
curl $OPTS "http://127.0.0.1:19090/run/request-response-codes"
curl $OPTS "http://127.0.0.1:19090/run/access"

# curl $OPTS "http://127.0.0.1:19090/demo/request-headers-out"
# curl $OPTS "http://127.0.0.1:19090/demo/delayed-output"
# curl $OPTS "http://127.0.0.1:19090/demo/delayed-output-2"
# curl $OPTS "http://127.0.0.1:19090/demo/return-an-error"
# curl $OPTS "http://127.0.0.1:19090/demo/return-error-500"
# curl $OPTS "http://127.0.0.1:19090/demo/random"
# curl $OPTS "http://127.0.0.1:19090/demo/msie6"
# curl $OPTS -I "http://127.0.0.1:19090/demo/handler"
# curl $OPTS "http://127.0.0.1:19090/demo/handler"
# curl $OPTS "http://127.0.0.1:19090/demo/subrequest-long"

# ab -kc 20 -n 200 http://127.0.0.1:19090/run/file


MASTER_PID=$(cat $PIDFILE)

if [ -f $PIDFILE ]; then
	PID=$(cat $PIDFILE)
	kill -QUIT $PID
	sleep 1
	if [ -f $PIDFILE ]; then
		kill -KILL $PID
	fi
fi


if [ "$WITH_MASTER" = "yes" ]; then
	if [ -f "$MODULE_ROOT/process-cycle-worker-exited-$TIME.txt" ]; then
		echo "exitWorker() worked"
		rm "$MODULE_ROOT/process-cycle-worker-exited-$TIME.txt"
	else
		echo "exitWorker() failed"
	fi
	
	if [ -f "$MODULE_ROOT/process-cycle-master-exited-$MASTER_PID.txt" ]; then
		echo "exitMaster() worked"
		rm "$MODULE_ROOT/process-cycle-master-exited-$MASTER_PID.txt"
	else
		echo "exitMaster() failed"
	fi
fi

echo